package Algorithm.DynamicPlanning;

import java.util.HashSet;
import java.util.Set;

public class Code15_LongestIntegratedLength {

    public static int getLIL(int[] arr){
        if (arr == null || arr.length == 0){
            return 0;
        }
        int len = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        Set<Integer> set = new HashSet<>(); // 存放当前子数组数值
        for (int i = 0; i < arr.length; i++) {
            set.clear();
            for (int j = i; j < arr.length; j++){
                if (set.contains(arr[j])){
                    break;
                } else {
                    set.add(arr[j]);
                    max = Math.max(max, arr[j]);
                    min = Math.min(min, arr[j]);
                    if (max - min != j - i){
                        break;
                    }
                    len = Math.max(len, j - i + 1);
                }
            }
            max = Integer.MIN_VALUE;
            min = Integer.MAX_VALUE;
        }
        return len;
    }

    public static void main(String[] args) {
        int[] arr = { 5, 5, 3, 2, 6, 4, 3 };
        //int[] arr = { 1,2,3,4,5 };
        System.out.println(getLIL(arr));

    }
}
